package pl.gettoknowme.mobile.android.services.calls;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import pl.gettoknowme.mobile.android.services.calls.CallLogEntry.CallType;
import pl.gettoknowme.mobile.android.util.MiscUtils;

import android.content.Context;
import android.database.Cursor;

/**
 * Czytnik historii połączeń
 * @author Michał Kołodziejski
 */
public class CallLogReader {

    protected Context context;
    
    public CallLogReader(Context context) {
        this.context = context;
    }
    
    public Map<CallType, List<CallLogEntry>> readCallLogs(){
        return doReadCallLogs(null);
    }
    
    public Map<CallType, List<CallLogEntry>> readCallLogs(long sinceTimestamp){
        return doReadCallLogs(Long.valueOf(sinceTimestamp));
    }
    
    
    protected Map<CallType, List<CallLogEntry>> doReadCallLogs(Long sinceTimestamp){
        String[] projection = {
                android.provider.CallLog.Calls.TYPE,
                android.provider.CallLog.Calls.DURATION,
                android.provider.CallLog.Calls.NUMBER,
                android.provider.CallLog.Calls.DATE
        };
        String where = null;
        String[] whereArgs = null;
        if(sinceTimestamp != null){
            where = android.provider.CallLog.Calls.DATE + " > ?";
            whereArgs = new String[1];
            whereArgs[0] = String.valueOf(sinceTimestamp.longValue());
        }
        String sortOrder = android.provider.CallLog.Calls.DATE + " ASC";
        
        Cursor cursor = context.getContentResolver().query(
                android.provider.CallLog.Calls.CONTENT_URI,
                projection, 
                where, 
                whereArgs, 
                sortOrder);
        
        Map<CallType, List<CallLogEntry>> resultMap = new HashMap<CallType, List<CallLogEntry>>();
        resultMap.put(CallType.INCOMING, new ArrayList<CallLogEntry>());
        resultMap.put(CallType.OUTGOING, new ArrayList<CallLogEntry>());
        resultMap.put(CallType.MISSED, new ArrayList<CallLogEntry>());
        resultMap.put(CallType.UNKNOWN, new ArrayList<CallLogEntry>());
        
        // podręczna mapa hashów numerów telefonów
        Map<String, String> numberHashMap = new HashMap<String, String>();
        
        while(cursor.moveToNext()){
            String number = cursor.getString(2);
            if(number == null){
                // just in case
                number = "";
            }
            number = MiscUtils.shortenPhoneNumber(number);
            
            // numer telefonu przechowujemy w postaci skrótu
            if(!numberHashMap.containsKey(number)){
                numberHashMap.put(number, MiscUtils.hashString(number));
            }
            String hashedNumber = numberHashMap.get(number);
            
            CallLogEntry entry = new CallLogEntry();
            entry.setCallType(CallType.forInt(cursor.getInt(0)));
            entry.setSeconds(cursor.getInt(1));
            entry.setNumber(hashedNumber);
            entry.setTimestamp(cursor.getLong(3));
            
            resultMap.get(entry.getCallType()).add(entry);
        }
        
        return resultMap;
    }
}
